home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xpick / xpick_11.z / xpick_11 / xpick-1.1 / xpick.c < prev    next >
C/C++ Source or Header  |  1993-06-21  |  6KB  |  230 lines

  1. /*
  2.  * Copyright 1993 The University of Newcastle upon Tyne
  3.  * 
  4.  * Permission to use, copy, modify and distribute this software and its
  5.  * documentation for any purpose other than its commercial exploitation
  6.  * is hereby granted without fee, provided that the above copyright
  7.  * notice appear in all copies and that both that copyright notice and
  8.  * this permission notice appear in supporting documentation, and that
  9.  * the name of The University of Newcastle upon Tyne not be used in
  10.  * advertising or publicity pertaining to distribution of the software
  11.  * without specific, written prior permission. The University of
  12.  * Newcastle upon Tyne makes no representations about the suitability of
  13.  * this software for any purpose. It is provided "as is" without express
  14.  * or implied warranty.
  15.  * 
  16.  * THE UNIVERSITY OF NEWCASTLE UPON TYNE DISCLAIMS ALL WARRANTIES WITH
  17.  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  18.  * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF
  19.  * NEWCASTLE UPON TYNE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  23.  * PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * Author:  Gerry Tomlinson (gerry.tomlinson@newcastle.ac.uk)
  26.  *          Computing Laboratory, University of Newcastle upon Tyne, UK
  27.  */
  28.  
  29. /*****************************************************************************
  30.  
  31. xpick.c
  32.  
  33. xpick using MultiList widget
  34.  
  35. Gerry Tomlinson Feb 1992
  36.  
  37.  ****************************************************************************/
  38.  
  39. #include <X11/Xlib.h>
  40. #include <X11/Intrinsic.h>
  41. #include <X11/StringDefs.h>
  42. #include <X11/Shell.h>
  43. #include <X11/Xaw/AsciiText.h>
  44. #include <X11/Xaw/Paned.h>
  45. #include <X11/Xaw/Viewport.h>
  46. #include <X11/Xaw/Command.h>
  47. #include <X11/Xaw/Label.h>
  48.  
  49. #include "MultiList.h"
  50.  
  51. #include <stdio.h>
  52.  
  53. #define MAXITEMS 2000
  54. static char *strings[MAXITEMS];
  55. static char **args;
  56.  
  57. static Widget top,vport,vpaned,hpaned,ok,set,clear,invert,pattern,multiList;
  58. static void Ok(), ApplyPattern();
  59.  
  60. main(argc,argv)
  61. int argc;
  62. char **argv;
  63. {
  64.  
  65.     XtAppContext app_con;
  66.     int cols = 0;
  67.  
  68.     char buf[BUFSIZ];
  69.     int i = 0;
  70.  
  71.         top = XtVaAppInitialize(&app_con,"Xpick",
  72.         NULL,0,
  73.         &argc,argv,NULL,
  74.         NULL);
  75.  
  76.     if (argc == 1) exit(0);
  77.  
  78.     if (argv[1][0] == '-' && argv[1][1]) {
  79.         cols = atoi(&argv[1][1]);
  80.         if (!cols) {
  81.         fprintf(stderr,"xpick: bad number of columns\n");
  82.         exit (1);
  83.         }
  84.         argc--; argv++;
  85.     }
  86.  
  87.     if (argc == 1) exit(0);
  88.  
  89.         if (argc == 2 && strcmp(argv[1], "-") == 0) { 
  90.         while (fgets(buf, sizeof buf, stdin) != NULL) {
  91.           buf[strlen(buf)-1] = '\0';
  92.         strings[i] = XtNewString(buf);
  93.         if (++i == MAXITEMS) {
  94.             fprintf(stderr,"xpick: too many input lines\n");
  95.             exit(1);
  96.         }
  97.         }
  98.         if (i == 0) exit(0);
  99.         strings[i] = 0;
  100.         args = strings;
  101.     }
  102.     else { i = argc ; args = &argv[1];}
  103.  
  104.  
  105.         vpaned = XtVaCreateManagedWidget("vpaned",panedWidgetClass,top,NULL);
  106.  
  107.         hpaned = XtVaCreateManagedWidget("hpaned",panedWidgetClass,vpaned,
  108.             NULL);
  109.  
  110.  
  111.         ok = XtVaCreateManagedWidget ("ok", commandWidgetClass,hpaned,
  112.                                 NULL);
  113.  
  114.         set = XtVaCreateManagedWidget ("set", commandWidgetClass,hpaned,
  115.                                 NULL);
  116.  
  117.  
  118.         clear = XtVaCreateManagedWidget ("clear",commandWidgetClass,hpaned,
  119.                                 NULL);
  120.  
  121.         invert = XtVaCreateManagedWidget ("invert",commandWidgetClass,hpaned,
  122.                                 NULL);
  123.  
  124.         pattern = XtVaCreateManagedWidget ("pattern",asciiTextWidgetClass,hpaned,
  125.                 XtNeditType,XawtextEdit,
  126.                                 NULL);
  127.  
  128.     vport = XtVaCreateManagedWidget ("viewport", viewportWidgetClass,vpaned,
  129.                     NULL);
  130.  
  131.         if (cols)
  132.             multiList = XtVaCreateManagedWidget("multiList",
  133.                                              multiListWidgetClass,vport,
  134.                                 XtNlist,args,
  135.                                 XtNnumberStrings,0,
  136.                                 XtNforceColumns,TRUE,
  137.                                 XtNmaxSelectable,i,
  138.                 XtNdefaultColumns,cols,
  139.                                 NULL);
  140.     else
  141.         multiList = XtVaCreateManagedWidget("multiList",
  142.                                              multiListWidgetClass,vport,
  143.                                 XtNlist,args,
  144.                                 XtNnumberStrings,0,
  145.                                 XtNforceColumns,FALSE,
  146.                                 XtNmaxSelectable,i,
  147.                 NULL);
  148.  
  149.  
  150.         XtAddCallback(ok,XtNcallback,Ok,(XtPointer)NULL);
  151.         XtAddCallback(set,XtNcallback,ApplyPattern,(XtPointer)MultiListHighlightItem);
  152.         XtAddCallback(clear,XtNcallback,ApplyPattern,(XtPointer)MultiListUnhighlightItem);
  153.         XtAddCallback(invert,XtNcallback,ApplyPattern,(XtPointer)MultiListToggleItem);
  154.  
  155.     XtInstallAccelerators(pattern,set);
  156.  
  157.         XtRealizeWidget(top);
  158.  
  159. /* reset XtNdefaultColumns to allow recomputation if user resizes  */
  160.     if (!cols)
  161.         XtVaSetValues(multiList,XtNdefaultColumns,0,NULL);
  162.  
  163.     XtSetKeyboardFocus(top,pattern);
  164.         XtAppMainLoop(app_con);
  165. } /* End main */
  166.  
  167.  
  168. /*ARGSUSED*/
  169. static void
  170. Ok(widget,client_data,call_data)
  171. Widget widget;
  172. XtPointer client_data;
  173. XtPointer call_data;
  174. {
  175.         int i;
  176.         String *str_ptr;
  177.         Boolean *h_ptr,*s_ptr;
  178.  
  179.     MultiListReturnStruct *rs;
  180.  
  181.         rs = MultiListGetHighlighted(multiList);
  182.  
  183.         for (i = 0; i < rs->num_selected; i++)
  184.         {
  185.         MultiListGetItemInfo(multiList,rs->selected_items[i],&str_ptr,&h_ptr,&s_ptr);
  186.                 printf("%s\n",str_ptr);
  187.     }
  188.     exit(0);
  189. }
  190.  
  191.  
  192. /*ARGSUSED*/
  193. static void ApplyPattern (w,client_data,call_data)
  194. Widget w;
  195. XtPointer client_data;
  196. XtPointer call_data;
  197. {
  198. typedef void (*multiListProc)(); 
  199.     extern Boolean match();
  200.     String p,m;
  201.     String empty = "*";
  202.     int i;
  203.     Boolean gotmatch = FALSE;
  204.  
  205.     XtVaGetValues(pattern, XtNstring, &p, NULL);
  206.     if (!p[0])
  207.         p = empty;
  208.  
  209.     m = XtNewString(p);
  210.     for(i=0; p[i]; i++) {
  211.         switch (p[i]) {
  212.         case '?':
  213.         case '*':
  214.         case '[':
  215.             m[i] = 1;
  216.             break;
  217.         default:
  218.             m[i] = 0;
  219.         }
  220.     }
  221.  
  222.     for (i=0; args[i];  i++) 
  223.         if (match(p, m, args[i])) {
  224.             ((multiListProc)client_data)(multiList,i);
  225.             gotmatch = TRUE;
  226.         }
  227.     if (!gotmatch) XBell(XtDisplay(top),0);
  228.     XtFree(m);
  229. }
  230.